package ynu.lcy.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Where;
import ynu.lcy.dto.food.AddFoodRequestDTO;
import ynu.lcy.elememicroservicecommon.entity.LogicEntity;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
@Setter
@Getter
@ToString
@NoArgsConstructor
@Where(clause = "is_deleted = 0")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Food extends LogicEntity {
    /**
     * 食品名称
     * */
    private String foodName;
    /**
     * 食品描述
     * */
    private String foodExplain;
    /**
     * 食品图片
     * */
    private String foodImg;
    /**
     * 食品价格
     * */
    private Double foodPrice;
    /*
     * 所在购物车
     * */
    @OneToMany(mappedBy = "food",
            fetch = FetchType.EAGER,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("food")
    private List<CartFoodItem> cartItems;


    /**
     * 所属商家
     * */
    private Long businessId;

    @JsonIgnore
    public Food create(AddFoodRequestDTO dto, String foodImg){
        this.setBusinessId(dto.getBusinessId());
        this.setFoodExplain(dto.getFoodExplain());
        this.setFoodName(dto.getFoodName());
        this.setFoodImg(foodImg);
        this.setFoodPrice(dto.getFoodPrice());
        this.setCreateTime(new Date());
        this.setIsDeleted(0);
        return this;
    }

}
